﻿$(document).ready(function() {

    // Function to retrieve the width of message text
    $.fn.textWidth = function() {
        var sensor = $('<div />').css({ margin: 0, padding: 0 });
        $(this).append(sensor);
        var width = sensor.width();
        sensor.remove();
        return width;
    };

    // Conditional to determine whether or not to show or hide the language bar
    if ($('#hdnMessageId').val().length > 0) {
        $('p.current-language-welcome').html($('#hdnMessageId').val());
        $(".language-header").show();
    } else {
        $(".language-header").hide();
    };

    // Retrieve the width of the welcome text and apply to container
    var messageWidth = $('p.current-language-welcome').textWidth() + 30;
    $(".language").css('width', messageWidth);

    // Set initial height of the language bar
    $(".language-header").css('height', '39px');

    // Wire up the Change button
    $(".current-language-change").click(function() {
        if ($(this).parent().parent().hasClass('open')) {
            $(this).parent().parent().animate({ height: '39px' }, 500);
            $(this).parent().parent().removeClass('open');
        } else {
            // Retrieve the width of the language options text and the visible links and apply to container
            var languageChoice = 0;
            $('.other-languages a').each(function() {
                languageChoice += $(this).outerWidth(true);
            });
            var optionsMessageWidth = languageChoice;
            $(".other-languages").css('width', optionsMessageWidth);
            $(this).parent().parent().animate({ height: '71px' }, 500);
            $(this).parent().parent().addClass('open');
        }
    });

    // Wire up the Close button
    $(".close-language-bar").click(function() {
        $(this).hide();
        $(".language-header").animate({ height: '0px' }, 500);
    });

});